﻿using System;
using System.Web.Security;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;

namespace FacialClub.Models.Repositories
{
    /// <summary>
    /// Account Repository
    /// </summary>
    public class AccountRepository : IAccountRepository
    {
        private readonly MembershipProvider _provider;
        readonly FacialClubEntities _db = new FacialClubEntities();
        /// <summary>
        /// Account Membership Service Constructor
        /// </summary>
        public AccountRepository() : this(null)
        {
        }

        /// <summary>
        /// Account Membership Service Overloaded Constructor
        /// </summary>
        /// <param name="provider"></param>
        public AccountRepository(MembershipProvider provider)
        {
            _provider = provider ?? Membership.Provider;
        }

        #region IAccountRepository Members

        /// <summary>
        /// SignIn
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="createPersistentCookie"></param>
        public void SignIn(string userName, bool createPersistentCookie)
        {
            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }

        /// <summary>
        /// SignOut
        /// </summary>
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

        /// <summary>
        /// Minimum Password Length
        /// </summary>
        public int MinPasswordLength
        {
            get { return _provider.MinRequiredPasswordLength; }
        }

        /// <summary>
        /// Validate User
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ValidateUser(string userName, string password)
        {
            return _provider.ValidateUser(userName, password);
        }

        /// <summary>
        /// Get Canonical Username
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string GetCanonicalUsername(string userName)
        {
            MembershipUser user = _provider.GetUser(userName, true);
            return user != null ? user.UserName : null;
        }

        /// <summary>
        /// Create User
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            _provider.CreateUser(userName, password, email, null, null, true, null, out status);
            return status;
        }

        /// <summary>
        /// Register Customer
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="birthDate"></param>
        /// <param name="address"></param>
        /// <param name="city"></param>
        /// <param name="state"></param>
        /// <param name="postalCode"></param>
        /// <param name="country"></param>
        /// <param name="telephone"></param>
        /// <param name="telephoneExtension"></param>
        /// <param name="fax"></param>
        /// <param name="faxExtension"></param>
        /// <param name="email"></param>
        /// <param name="url"></param>
        /// <param name="isActive"></param>
        /// <param name="userCreated"></param>
        /// <param name="createdDate"></param>
        public void RegisterUser(Guid userId, string firstName, string lastName, DateTime birthDate, string address, string city, string state, string postalCode, string country, string telephone, string telephoneExtension, string fax, string faxExtension, string email, string url, bool isActive, string userCreated, DateTime createdDate)
        {
            var customer = new Customer
                               {
                                   UserId = userId,
                                   FirstName = firstName,
                                   LastName = lastName,
                                   BirthDate = birthDate,
                                   Address = address,
                                   City = city,
                                   State = state,
                                   PostalCode = postalCode,
                                   Country = country,
                                   Telephone = telephone,
                                   TelephoneExtension = telephoneExtension,
                                   Fax = fax,
                                   FaxExtension = faxExtension,
                                   Email = email,
                                   URL = url,
                                   IsActive = isActive,
                                   UserCreated = userCreated,
                                   CreatedDate = createdDate
                               };
            _db.Customers.AddObject(customer);
            _db.SaveChanges();
        }

        /// <summary>
        /// Change Password
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
            return currentUser.ChangePassword(oldPassword, newPassword);
        }

        /// <summary>
        /// Get UserId By Email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public Guid GetUserIdByEmail(string email)
        {
            var userId = (Guid)Membership.GetUser(Membership.GetUserNameByEmail(email)).ProviderUserKey;
            return userId;
        }

        #endregion
    }
}